from utils import platform


def get_ifconfig(key=None):
    ifconfig = {"ip": "-", "mask": "-", "gateway": "-", "dns": "-"}

    if platform.is_esp32_series():
        config = esp32_get_ifconfig()
        ifconfig["ip"] = config[0]
        ifconfig["mask"] = config[1]
        ifconfig["gateway"] = config[2]
        ifconfig["dns"] = config[3]
        ifconfig["mac"] = esp32_get_mac_str()

    if key:
        return ifconfig[key]

    return ifconfig

def esp32_get_mac_str():
    import network

    wlan = network.WLAN(network.STA_IF)
    mac = wlan.config('mac')
    return ':'.join('{:02x}'.format(b) for b in mac)

def esp32_get_ifconfig():
    import network

    wlan = network.WLAN(network.STA_IF)
    return wlan.ifconfig()


if __name__ == "__main__":
    ifconfig = get_ifconfig()
    print(ifconfig)
